package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.Config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.OrganizationLevel;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.ConfigMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.config.JsmConfigService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.file.JsmFileService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.util.OrgTreeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 二次组态接口实现类
 * @CreateDateTime 2017-05-02 9:00
 **/
public class JsmConfigServiceImpl implements JsmConfigService {
    @Autowired
    private ConfigMapper configMapper;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private JsmFileService fileService;

    /**
     * 根据ID删除
     *
     * @param configId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String configId) {
        return configMapper.deleteByPrimaryKey(configId);
    }

    /**
     * 添加
     *
     * @param record
     * @return
     */
    @Override
    public int insert(Config record) {
        return configMapper.insert(record);
    }

    /**
     * 非空添加
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(Config record) {
        return 0;
    }

    /**
     * 根据ID查询
     *
     * @param configId
     * @return
     */
    @Override
    public Config selectByPrimaryKey(String configId) {
        return configMapper.selectByPrimaryKey(configId);
    }

    /**
     * 非空修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Config record) {
        return configMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKey(Config record) {
        return configMapper.updateByPrimaryKey(record);
    }

    /**
     * @Author 卢曼成
     * @Description 根据条件查询集合
     * @CreateDateTime 2017/5/2 8:56
     **/
    @Override
    public List<Config> selectByParamList(Config record) {
        return configMapper.selectByParamList(record);
    }

    /**
     * @param record
     * @return JSONObject
     * @throws Exception
     * @Description 分页查询
     * @Author 卢曼成
     */
    @Override
    public JSONObject findByPageList(Config record) throws Exception {
        List<Config> list = configMapper.queryList(record);
        int count = configMapper.queryCount(record);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", count);
        jsonObject.put("rows", JSON.toJSON(list));
        return jsonObject;
    }

    /**
     * @param employee
     * @return List<Ztree>
     * @Author 卢曼成
     * @Description 获取初始化组织树形
     * @CreateDateTime 2017/5/2 8:56
     */
    @Override
    public List<Ztree> initOrgTree(Employee employee) throws Exception {
        List<Organization> orgList = organizationService.getOrganizationList(employee, OrganizationLevel.ORG_LEVEL_BRANCH.getCode(), OrganizationLevel.ORG_LEVEL_WELL.getCode());
        Organization organization = new Organization();
        organization.setOrgLevelId(OrganizationLevel.ORG_LEVEL_WELL.getCode());
        List<Organization> orgItemList = organizationService.selectByOrgItem(organization);
        List<Ztree> demotree = new ArrayList<Ztree>();
        OrgTreeUtil.orgToTree(orgList, orgItemList, demotree, employee.getOrgId());
        return demotree;
    }

    /**
     * @param param
     * @return int
     * @Description 保存
     * @Author 卢曼成
     */
    @Override
    public int save(Config param) throws Exception {
        String configId = UniqueUtil.uuid();
        param.setConfigId(configId);
        Date date = new Date();
        param.setCreateDT(date);
        param.setOperateDT(date);
        fileService.addFile(param.getUploadFiles(), param.getDeletedFiles(), configId);
        return configMapper.insert(param);
    }

    /**
     * @param param
     * @return int
     * @Description 修改
     * @Author 卢曼成
     */
    @Override
    public int edit(Config param) throws Exception {
        if(StringUtils.isNotEmpty(param.getConfigId())){
            Config c = this.selectByPrimaryKey(param.getConfigId());
            c.setConfigName(param.getConfigName());
            c.setOrgId(param.getOrgId());
            c.setOperateId(param.getOperateId());
            c.setOperateDT(new Date());
            fileService.updateFile(param.getUploadFiles(), param.getDeletedFiles(), param.getConfigId());
            return configMapper.updateByPrimaryKeySelective(c);
        }
        return 0;
    }

    /**
     * 根据ID删除
     *
     * @param list
     * @return
     */
    @Override
    public int deleteByKeyList(List<String> list) {
        return configMapper.deleteByKeyList(list);
    }

}
